<?php
/**
 * @Name: helpers.php.
 * @author：yashuai
 */


/**
 * @Desc 成功返回
 * @param array $data
 * @param string $msg
 * @return \Illuminate\Http\JsonResponse
 * @author yashuai
 */
if (!function_exists('_success')) {
    function _success($data = [], $msg = 'success')
    {
        return response()->json([
            "code"   => 200,
            "msg"    => $msg,
            "result" => $data
        ]);
    }
}

/**
 * @Desc 失败返回
 * @param array $data
 * @param string $msg
 * @return \Illuminate\Http\JsonResponse
 * @author yashuai
 */
if (!function_exists('_fail')) {
    function _fail($code = 404, $msg = 'fail', $data = [])
    {
        $msg = empty($msg) ? \App\Http\ResponseCode::CodeGetMsg($code) : $msg;
        return response()->json([
            "code"   => $code,
            "msg"    => $msg,
            "result" => $data
        ]);
    }
}


/**
 * @Desc 失败抛出异常
 * @param int $code
 * @param array $data
 * @param string $msg
 * @author yashuai
 */
if (!function_exists('_throw')) {
    function _throw($code = 500, $msg = '', $data = [])
    {
        throw new \App\Exceptions\ApiException($msg, $code, $data);
    }
}

/**
 * @Desc 获取数组字段值
 * @param array $array
 * @param string $field
 * @param bool $default
 * @return array|bool|int|mixed|string
 * @author yashuai
 */
if (!function_exists('_array_field')) {
    function _array_field($array = [], $field = '', $default = false, $exception=true)
    {
        return \HugCode\PhpUnits\Utils\UtilArray::arrayField($array, $field, $default, $exception);
    }
}

/**
 * @Desc 获取当前控制器、方法名
 * @return array
 * @author yashuai
 */
if (!function_exists('_controller_action')) {
    function _controller_action()
    {
        $router = request()->route()->getActionName();
        $router = array_slice(explode('\\', $router), -1, 1)[0];
        list($controller, $action) = explode('@', $router);
        return [strtolower(str_replace('Controller', '', $controller)), strtolower($action)];
    }
}


/**
 * 值是否(只有0和1)
 * @param $value
 * @return int
 */
if (!function_exists('_is_value')) {
    function _is_value($value)
    {
        return $value == 1 ? 1 : 0;
    }
}

/**
 * 打印数组并断点
 * @param $param
 */
if (!function_exists('_export_exit')) {
    function _export_exit($param)
    {
        echo '<pre>';
        var_export($param);
        echo '<pre>';
        exit();
    }
}

/**
 * 打印数组
 * @param $param
 */
if (!function_exists('_export')) {
    function _export($param)
    {
        echo '<pre>';
        var_export($param);
        echo '<pre>';
    }
}





